package com.example.appp;

import java.util.Random;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

public class Spirit extends View {

    private static final int SIZE_STEP = 5;
    private final int ROAD = 777;
    private final int EMPTINESS = 16;
    private final int SPEED = 5;
    private static final int COUNT_HEIGHT_BLOCK = 24;
    private static final int COUNT_WIDTH_BLOCK = 16;

    Bitmap ghost;

    public enum way {
        UP, DOWN, LEFT, RIGHT, ZERO
    }

    way myWay;

    public Spirit(Context context, int size, int x, int y) {
        super(context);

        positionX = x * size;
        positionY = y * size;
        step = size / SIZE_STEP;
        countStep = size / SIZE_STEP;
        ghost = BitmapFactory.decodeResource(getResources(),
                R.drawable.blinky_4);
        myWay = way.UP;
        rectangleSpirit = new Rect();
        random = new Random();
    }

    void onLoadImageaBlinky() {
        if (myWay == way.LEFT) {
            ghost = BitmapFactory.decodeResource(getResources(),
                    R.drawable.blinky_3);
        }
        if (myWay == way.RIGHT) {
            ghost = BitmapFactory.decodeResource(getResources(),
                    R.drawable.blinky_4);
        }
        if (myWay == way.DOWN) {
            ghost = BitmapFactory.decodeResource(getResources(),
                    R.drawable.blinky_2);
        }
        if (myWay == way.UP) {
            ghost = BitmapFactory.decodeResource(getResources(),
                    R.drawable.blinky_1);
        }
    }

    void onLoadImageaPinky() {
        if (myWay == way.LEFT) {
            ghost = BitmapFactory.decodeResource(getResources(),
                    R.drawable.pinky_3);
        }
        if (myWay == way.RIGHT) {
            ghost = BitmapFactory.decodeResource(getResources(),
                    R.drawable.pinky_4);
        }
        if (myWay == way.DOWN) {
            ghost = BitmapFactory.decodeResource(getResources(),
                    R.drawable.pinky_2);
        }
        if (myWay == way.UP) {
            ghost = BitmapFactory.decodeResource(getResources(),
                    R.drawable.pinky_1);
        }
    }

    void onLoadImageaClyde() {
        if (myWay == way.LEFT) {
            ghost = BitmapFactory.decodeResource(getResources(),
                    R.drawable.clyde_3);
        }
        if (myWay == way.RIGHT) {
            ghost = BitmapFactory.decodeResource(getResources(),
                    R.drawable.clyde_4);
        }
        if (myWay == way.DOWN) {
            ghost = BitmapFactory.decodeResource(getResources(),
                    R.drawable.clyde_2);
        }
        if (myWay == way.UP) {
            ghost = BitmapFactory.decodeResource(getResources(),
                    R.drawable.clyde_1);
        }
    }

    void onLoadImageaInky() {
        if (myWay == way.LEFT) {
            ghost = BitmapFactory.decodeResource(getResources(),
                    R.drawable.inky_3);
        }
        if (myWay == way.RIGHT) {
            ghost = BitmapFactory.decodeResource(getResources(),
                    R.drawable.inky_4);
        }
        if (myWay == way.DOWN) {
            ghost = BitmapFactory.decodeResource(getResources(),
                    R.drawable.inky_2);
        }
        if (myWay == way.UP) {
            ghost = BitmapFactory.decodeResource(getResources(),
                    R.drawable.inky_2);
        }
    }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawBitmap(ghost, positionX, positionY, new Paint());
    }

    int step, countStep;
    int positionX, positionY;
    Rect rectangleSpirit;

    way isCollisionLeft(int[][] map, int size, way enumWay) {

        if (enumWay == way.LEFT) {
            if ((positionX % size != 0 || (map[positionY / size][positionX
                    / size - 1] == 0
                    || map[positionY / size][positionX / size - 1] == 1 || map[positionY
                    / size][positionX / size - 1] == EMPTINESS)
                    && (positionY % size == 0 || (map[positionY / size + 1][positionX
                            / size - 1] == 0
                            || map[positionY / size + 1][positionX / size - 1] == 1 || map[positionY
                            / size + 1][positionX / size - 1] == EMPTINESS)))) {
                return way.LEFT;
            }

        }

        if (enumWay == way.RIGHT) {
            if ((map[positionY / size][positionX / size + 1] == 0
                    || map[positionY / size][positionX / size + 1] == 1 || map[positionY
                    / size][positionX / size + 1] == EMPTINESS)
                    && (positionY % size == 0 || (map[positionY / size + 1][positionX
                            / size + 1] == 0
                            || map[positionY / size + 1][positionX / size + 1] == 1 || map[positionY
                            / size + 1][positionX / size + 1] == EMPTINESS))) {
                return way.RIGHT;
            }
        }

        if (enumWay == way.DOWN) {
            if ((map[positionY / size + 1][positionX / size] == 0
                    || map[positionY / size + 1][positionX / size] == 1 || map[positionY
                    / size + 1][positionX / size] == EMPTINESS)
                    && (positionX % size == 0 || (map[positionY / size + 1][positionX
                            / size + 1] == 0
                            || map[positionY / size + 1][positionX / size + 1] == 1 || map[positionY
                            / size + 1][positionX / size + 1] == EMPTINESS))) {
                return way.DOWN;
            }
        }

        if (enumWay == way.UP) {
            if (positionY % size != 0
                    || (map[positionY / size - 1][positionX / size] == 0
                            || map[positionY / size - 1][positionX / size] == 1 || map[positionY
                            / size - 1][positionX / size] == EMPTINESS)
                    && (positionX % size == 0 || (map[positionY / size - 1][positionX
                            / size + 1] == 0
                            || map[positionY / size - 1][positionX / size + 1] == 1 || map[positionY
                            / size - 1][positionX / size + 1] == EMPTINESS))) {
                return way.UP;
            }
        }

        return way.ZERO;
    }

    void onMove(int[][] map, int size) {

        if (isCollisionLeft(map, size, myWay) == way.LEFT) {
            positionX -= SPEED;
            step++;
            if (step > countStep) {
                step = 1;
            }
        }
        if (isCollisionLeft(map, size, myWay) == way.RIGHT) {
            positionX += SPEED;
            step++;
            if (step > countStep) {
                step = 1;
            }
        }
        if (isCollisionLeft(map, size, myWay) == way.DOWN) {
            positionY += SPEED;
            step++;
            if (step > countStep) {
                step = 1;
            }
        }
        if (isCollisionLeft(map, size, myWay) == way.UP) {
            positionY -= SPEED;
            step++;
            if (step > countStep) {
                step = 1;
            }
        }
        if (positionX == 15 * size - 10 && positionY == 11 * size
                && myWay == way.RIGHT) {
            positionX = 0;
        }
        if (positionX == 10 && positionY == 11 * size && myWay == way.LEFT) {
            positionX = 15 * size;
        }
        rectangleSpirit.set(positionX, positionY, positionX + size, positionY
                + size);
    }

    void findPathBlinky(int[][] map, int size) {
        int mystep = map[positionX / size][positionY / size];
        map[positionX / size][positionY / size] = ROAD;
        while (mystep > 1) {
            for (int row = 0; row < COUNT_WIDTH_BLOCK; row++) {
                for (int column = 0; column < COUNT_HEIGHT_BLOCK; column++) {
                    if (map[row][column] == ROAD) {

                        if (map[row - 1][column] == mystep - 1) {
                            map[row - 1][column] = ROAD;
                        } else if (map[row + 1][column] == mystep - 1) {
                            map[row + 1][column] = ROAD;
                        } else if (map[row][column - 1] == mystep - 1) {
                            map[row][column - 1] = ROAD;
                        } else if (map[row][column + 1] == mystep - 1) {
                            map[row][column + 1] = ROAD;
                        }

                    }
                }
            }
            mystep--;
        }
        if (map[positionX / size - 1][positionY / size] == ROAD
                && step == size / SPEED) {
            myWay = way.LEFT;
            step = 0;
            map[positionX / size - 1][positionY / size] = 0;
        } else if (map[positionX / size + 1][positionY / size] == ROAD
                && step == size / SPEED) {
            myWay = way.RIGHT;
            step = 0;
            map[positionX / size + 1][positionY / size] = 0;
        } else if (map[positionX / size][positionY / size - 1] == ROAD
                && step == size / SPEED) {
            myWay = way.UP;
            step = 0;
            map[positionX / size][positionY / size - 1] = 0;
        } else if (map[positionX / size][positionY / size + 1] == ROAD
                && step == size / SPEED) {
            myWay = way.DOWN;
            step = 0;
            map[positionX / size][positionY / size + 1] = 0;
        }
    }

    int countFindStep;
    int arrayRotate[] = new int[10];

    void findRotation(int[][] map, int size) {
        for (int index = 0; index < 10; index++) {
            arrayRotate[index] = 555;
        }
        countFindStep = 0;

        if (isCollisionLeft(map, size, myWay) != way.RIGHT && step == countStep) {
            arrayRotate[countFindStep] = 2;
            countFindStep++;
        }
        if (isCollisionLeft(map, size, myWay) != way.LEFT && step == countStep) {
            arrayRotate[countFindStep] = 3;
            countFindStep++;
        }
        if (isCollisionLeft(map, size, myWay) != way.UP && step == countStep) {
            arrayRotate[countFindStep] = 1;
            countFindStep++;
        }
        if (isCollisionLeft(map, size, myWay) != way.DOWN && step == countStep) {
            arrayRotate[countFindStep] = 0;
            countFindStep++;
        }

    }

    Random random;

    void findPathClyde(int[][] map, int size, int X, int Y) {
        findRotation(map, size);
        int wayX = 0, wayY = 0;
        if (countFindStep > 0) {
            if (Y > positionY) {
                wayY = 1;
            } else if (Y < positionY) {
                wayY = 0;
            } else if (X < positionX) {
                wayX = 3;
            } else if (X > positionX) {
                wayX = 2;
            }
            if ((wayX == arrayRotate[0]) || (wayX == arrayRotate[1])
                    || (wayX == arrayRotate[2])) {
                arrayRotate[countFindStep] = wayX;
                arrayRotate[countFindStep++] = wayX;
            } else if ((wayY == arrayRotate[0]) || (wayY == arrayRotate[1])
                    || (wayY == arrayRotate[2])) {
                arrayRotate[countFindStep] = wayY;
                arrayRotate[countFindStep++] = wayY;
            }
            int side = random.nextInt(countFindStep);
            if (arrayRotate[side] == 0) {
                myWay = way.UP;
            }
            if (arrayRotate[side] == 1) {
                myWay = way.DOWN;
            }
            if (arrayRotate[side] == 2) {
                myWay = way.LEFT;
            }
            if (arrayRotate[side] == 3) {
                myWay = way.RIGHT;
            }

        }
    }

    int side;
    way booferWay = null;

    void findPathInky(int[][] map, int size) {

        if (side == 0) {
            booferWay = way.UP;
        }
        if (side == 1) {
            booferWay = way.DOWN;
        }
        if (side == 2) {
            booferWay = way.LEFT;
        }
        if (side == 3) {
            booferWay = way.RIGHT;
        }

        if (side == 2) {
            if (isCollisionLeft(map, size, booferWay) == way.LEFT) {
                myWay = way.LEFT;
            } else
                side = random.nextInt(2);
        }
        if (side == 3) {
            if (isCollisionLeft(map, size, booferWay) == way.RIGHT) {
                myWay = way.RIGHT;
            } else
                side = random.nextInt(2);
        }

        if (side == 1) {
            if (isCollisionLeft(map, size, booferWay) == way.DOWN) {
                myWay = way.DOWN;
            } else
                side = random.nextInt(2) + 2;
        }
        if (side == 0) {
            if (isCollisionLeft(map, size, booferWay) == way.UP) {
                myWay = way.UP;
            } else
                side = random.nextInt(2) + 2;
        }

    }

}
